package cn.egame.extraction.multithread.file;

import java.util.HashMap;
import java.util.Map;

import cn.egame.extraction.multithread.util.ConstVar_EFS;

/**
 * Description 文件操作类, 多例模式
 * 
 * @Author yuchao
 * 
 * @Create Date 2014-4-3
 * 
 * @Modified by none
 * 
 * @Modified Date
 */
public class FileServiceHandler {
	private static Map<String, FileService> hash = new HashMap<String, FileService>();

	static {
		// synchronized (FileHandler.class) {
		// 初始化文件操作类实例
		for (int i = 0; i < ConstVar_EFS.FETCH_THREAD_NUM; i++) {
			String filename = ConstVar_EFS.THREAD_FETCH_BASE_FILE_PATH + i;
			if (!hash.containsKey(filename)) {
				FileService fileService = new FileService();
				hash.put(filename, fileService);
			}
		}

		String maxIdFilename = ConstVar_EFS.MAX_ID_FILE_PATH;
		if (!hash.containsKey(maxIdFilename)) {
			FileService fileService = new FileService();
			hash.put(maxIdFilename, fileService);
		}
		
		String errorFile404 = ConstVar_EFS.ERROR_IDS_FILE_PATH_404;
		if (!hash.containsKey(errorFile404)) {
			FileService fileService = new FileService();
			hash.put(errorFile404, fileService);
		}
		
		String errorFile500 = ConstVar_EFS.ERROR_IDS_FILE_PATH_500;
		if (!hash.containsKey(errorFile500)) {
			FileService fileService = new FileService();
			hash.put(errorFile404, fileService);
		}
		// }

	}

	public static FileService getInstance(String filename) {
		FileService instance = (FileService) hash.get(filename);
		// if (instance == null) {
		// synchronized (lock) {
		// instance = (FileService) hash.get(filename);
		// if (instance == null) {
		// instance = init(server);
		// instance = new CacheClientAdpter((CacheClientBase)instance);
		// hash.put(server, instance);
		// }
		// }
		// }
		return instance;
	}

	public static void getSize() {
		System.out.println(hash.size());
	}
}
